package com.ybg.bookmanages.commons;

import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class UploadUtil {

    public String upload(String imgPath){
        //取出图片后缀名
        String suffix=imgPath.substring(imgPath.lastIndexOf(".")+1);
        String[] jpgs = null;
        try {
            ClientGlobal.init("fastdfs.conf");
            TrackerClient tc = new TrackerClient();
            TrackerServer server = tc.getConnection();
            StorageServer stserver = null;
            StorageClient stclient = new StorageClient(server, stserver);
            jpgs = stclient.upload_file(imgPath, suffix, null);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return jpgs[0]+"/"+jpgs[1];
    }
}
